import Image
import matplotlib

def compareImageHist(im1, im2, cropBox=None,legendText=None):
    if cropBox!=None:
        im1 = im1.crop(cropBox)
        im2 = im2.crop(cropBox)

    red1, green1, blue1 = im1.split()
    red2, green2, blue2 = im2.split()

    red1, green1, blue1 = im1.split()
    red2, green2, blue2 = im2.split()

    red1Hist=red1.histogram()
    green1Hist=green1.histogram()
    blue1Hist=blue1.histogram()

    red2Hist=red2.histogram()
    green2Hist=green2.histogram()
    blue2Hist=blue2.histogram()

    matplotlib.pyplot.figure()
    matplotlib.pyplot.plot(red1Hist,'r')
    matplotlib.pyplot.hold(True)
    matplotlib.pyplot.plot(red2Hist,'b')
    if legendText!=None:
        matplotlib.pyplot.legend(legendText)
    matplotlib.pyplot.title('Comparing the red color channels',fontsize='xx-large')
    matplotlib.pyplot.xlabel('pixel value',fontsize='xx-large')
    matplotlib.pyplot.ylabel('# of pixels',fontsize='xx-large')
    matplotlib.pyplot.xlim((0,255))


    matplotlib.pyplot.figure()
    matplotlib.pyplot.plot(green1Hist,'r')
    matplotlib.pyplot.hold(True)
    matplotlib.pyplot.plot(green2Hist,'b')
    if legendText!=None:
        matplotlib.pyplot.legend(legendText)
    matplotlib.pyplot.title('Comparing the green color channels',fontsize='xx-large')
    matplotlib.pyplot.xlabel('pixel value',fontsize='xx-large')
    matplotlib.pyplot.ylabel('# of pixels',fontsize='xx-large')
    matplotlib.pyplot.xlim((0,255))

    matplotlib.pyplot.figure()
    matplotlib.pyplot.plot(blue1Hist,'r')
    matplotlib.pyplot.hold(True)
    matplotlib.pyplot.plot(blue2Hist,'b')
    if legendText!=None:
        matplotlib.pyplot.legend(legendText)
    matplotlib.pyplot.title('Comparing the blue color channels',fontsize='xx-large')
    matplotlib.pyplot.xlabel('pixel value',fontsize='xx-large')
    matplotlib.pyplot.ylabel('# of pixels',fontsize='xx-large')
    matplotlib.pyplot.xlim((0,255))
